package com.hengtongxing.hejiayun_employee.mine;


import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.google.gson.Gson;
import com.gyf.immersionbar.ImmersionBar;
import com.hengtongxing.hejiayun_employee.R;
import com.hengtongxing.hejiayun_employee.base.BaseFragment;
import com.hengtongxing.hejiayun_employee.base.DataCallBack;
import com.hengtongxing.hejiayun_employee.bean.UserInfoBean;
import com.hengtongxing.hejiayun_employee.event.UpDateNameEvent;
import com.hengtongxing.hejiayun_employee.homepage.model.HomePageModelImpl;
import com.hengtongxing.hejiayun_employee.homepage.model.IHomePageModel;
import com.hengtongxing.hejiayun_employee.login.LoginActivity;
import com.hengtongxing.hejiayun_employee.mine.model.MineModelImpl;
import com.hengtongxing.hejiayun_employee.utils.SPManager;
import com.hengtongxing.hejiayun_employee.widget.CommonNoTitleDialog;

import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 我的
 */
public class MineFragment extends BaseFragment {
    @BindView(R.id.iv_header)
    ImageView ivHeader;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_mobile)
    TextView tvMobile;
    @BindView(R.id.ll_setting)
    LinearLayout llSetting;
    @BindView(R.id.ll_exit)
    LinearLayout llExit;
    @BindView(R.id.tv_type_name)
    TextView tvTypeName;
    private Unbinder unbinder;
    private UserInfoBean userInfoBean;
    private CommonNoTitleDialog.Builder dialogBuild;
    private IHomePageModel homePageModel;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mine, container, false);
        unbinder = ButterKnife.bind(this, view);
        registerEventBus();
        initView();
        return view;
    }

    @Override
    public void initImmersionBar() {
        ImmersionBar.with(this)
                .statusBarDarkFont(false)
                .navigationBarDarkIcon(true)
                .navigationBarColor(R.color.white)
                .statusBarDarkFont(false, 0.2f)
                .init();
    }


    public void initView() {
        homePageModel = new HomePageModelImpl();
        requestUserInfo();
    }

    /**
     * 用户信息
     */
    public void requestUserInfo() {
        new MineModelImpl().requestMine(new DataCallBack<UserInfoBean>() {
            @Override
            public void onSuccessful(UserInfoBean bean) {
                userInfoBean = bean;
                tvName.setText(userInfoBean.getData().getName());
                tvTypeName.setText(userInfoBean.getData().getType_text());
                if (!TextUtils.isEmpty(userInfoBean.getData().getMobile()) && userInfoBean.getData().getMobile().length() == 11) {
                    String mobile = userInfoBean.getData().getMobile().substring(0, 3) + "****" + userInfoBean.getData().getMobile().substring(7, 11);
                    tvMobile.setText(mobile);
                } else {
                    tvMobile.setText(userInfoBean.getData().getMobile());
                }
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    @Subscribe
    public void onUpDateNameEvent(UpDateNameEvent event) {
        userInfoBean = new Gson().fromJson(SPManager.getInstance().getUserInfoJson(), UserInfoBean.class);
        if (userInfoBean != null) {
            tvName.setText(userInfoBean.getData().getName());
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        requestUserInfo();
    }

    private boolean isViewCreated; // 界面是否已创建完成
    private boolean isVisibleToUser; // 是否对用户可见

    /**
     * Fragment 创建期间setUserVisibleHint()方法是在onActivityCreated()之前被调用
     */
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        this.isVisibleToUser = isVisibleToUser;
        tryLoadData();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        isViewCreated = true;
        tryLoadData();
    }

    public void tryLoadData() {
        if (isViewCreated && isVisibleToUser) {
            requestUserInfo();
        }
    }

    @OnClick({R.id.ll_setting, R.id.ll_exit, R.id.ll_about_us})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_setting:
                skipToActivity(SettingActivity.class);
                break;
            case R.id.ll_exit:
                dialogBuild = new CommonNoTitleDialog.Builder(mActivity);
                dialogBuild.setMessage("是否退出登录?");
                dialogBuild.setPositiveButton("确定", v -> {
                    dialogBuild.dismiss();
                    outLogin();
                });
                dialogBuild.setNegativeButton("取消", v -> dialogBuild.dismiss());
                dialogBuild.setCancelable(false);
                dialogBuild.show();
                break;
            case R.id.ll_about_us:
                skipToActivity(AboutUsActivity.class);
                break;
        }
    }

    /**
     * 退出登录
     */
    public void outLogin() {
        SPManager.getInstance().logOffRemove();
        Intent intent = new Intent(mActivity, LoginActivity.class);
        startActivity(intent);
        mActivity.finish();
    }
}
